<?php
/**
 * Description: 盲盒10点推送
 * Created by PhpStorm.
 * User: congyukun
 * Date: 2021/4/1
 * Time: 11:22
 * @version  4.25.0
 */

use Biz\service\brand\BlindBox;
use Doraemon\tools\Hhzurl;
use Doraemon\tools\LinkFormat;
use hhz\constant\redis\redisKeys;
use Hhz\Redis\otherRedis;

error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ^ E_DEPRECATED);
ini_set('display_errors', 1);
require_once '/data/wwwroot/g_vendor/autoload.php';


//测试
//$rKey = 'blind_box_sub_' . date("Y-m-d");
//$count = \Biz\cache\brand\BlindBox::sadd($rKey, array(12042960));
//$uids = \Biz\cache\brand\BlindBox::smembers($rKey);
//echo "</pre>";
//print_r($uids);
//echo "</pre>";
//die();


$prizeList = BlindBox::getPrizeList(0);
unset($prizeList['-1'], $prizeList['-2'], $prizeList[0]);

$title_arr = array_column($prizeList, 'prize_title');

$title = implode('、', $title_arr);


$start = time();
$page = 1;
$pageSize = 1000;
$list = [];
try {

    $count = \Biz\cache\brand\BlindBox::getSubPushCount();
//    //迁移使用新的redis方案
//    $constKey = redisKeys::KEY_B_OTHER_BLIND_BOX_SUB_;
//    $redis = new otherRedis($constKey);
//    $sKey = $constKey. date("Y-m");
//    $count = $redis->smembers($sKey);

    if ($count <= 0) {
        \Biz\cache\brand\BlindBox::delSubPush();
//        $redis->del($sKey);
        return true;
    }

    $channel = \Doraemon\service\Push\PushService::getRuleByType('subscribe', '10:00');
    do {
        $uids = \Biz\cache\brand\BlindBox::getSubPushUids();
//        $uids = $redis->smembers($sKey);


        //偏移量，当前页-1乘以每页显示条数
        $start = ($page - 1) * $pageSize;
        //每页的wiki_id集合
        $ids = array_slice($uids, $start, $pageSize);
        foreach ($ids as $id) {
            pushMsg($id, $title, $channel);
        }

        if (!$ids) {
            \Biz\cache\brand\BlindBox::delSubPush();
//            $redis->del($sKey);
        }
        $page++;
    } while ($ids);

} catch (\Exception $e) {
    \Doraemon\tools\Log::error('品牌图片管理列表-error:' . $e->getMessage(), $list);
}
$end = time();
var_dump(date('Y-m-d-H-i-s') . '清洗数据完成,总耗时:' . ($end - $start) . '秒，' . (($end - $start) / 60) . '分');


function pushMsg($uid, $title, $channel)
{
    $link = Hhzurl::getDomain() . '/brand/blind-box?hhzjump=1';
//    $link = LinkFormat::encryptionLink($link);
    $link = \Biz\tools\helper\Common::formatLinkNormalLink($link);
    $pushId = \Doraemon\pockets\constInterface\Push\PushId::BRAND_BLIND_BOX_SUB . date('Y-m-d');
    $params = [
        'uid' => $uid,
        'title' => '叮～今日盲盒已准备好，快来抽取盲盒吧！',
        'alert' => $title . '正在抽奖，快来带走Ta吧！',
        'param' => [
            'link' => $link,
            'push_id' => $pushId,
            'template_id' => 11,
            'image' => '',
            'channel' => $channel
        ]
    ];
    $res = \Doraemon\service\Push\PushService::send($params);
    var_dump($res);
}
